var api = require('../../../utils/api.js')
var $ = require('../../../utils/util.js')
var regeneratorRuntime = require('../../../utils/runtime.js')
var app = getApp()
var Store = app.Store

Page({
  data: {
    form:{}, // redux传过来的参数
    pageList: [
    ], // 页面要展示的数据
    isShow: false,  
    titleType:'添加', // 当前选中的select [id] 
    controlMeasures:'',
    desNum:'',
    riskLevel:0,
    riskType:'',
    mainType:'',
    operateType:'',
    ticketNumber:'',
  },
  onLoad: function (options) {
    this.setData({
      form: Store.getState().formInfor.form
    })  
    switch (this.data.form.key) {
      case 'riskMeasures': 
      this.setData({
        riskType:this.data.form.config.riskType,
        mainType:this.data.form.config.mainType,
        riskLevel:this.data.form.config.riskLevel, 
        pageList:this.data.form.config.riskControl,
        operateType:this.data.form.config.operateType,
        ticketNumber:this.data.form.config.ticketNumber,
      })
        break;
    }
  },
  onShow: function () {
    
  },
//  工作票，操作票---开始
selectRiskLevel(){
  var that=this
  wx.showActionSheet({
    itemList: ['1级', '2级','3级', '4级','5级','6级'],
    success (res) {
      console.log(res)
      that.setData({
        "riskLevel":res.tapIndex+1+'级'
      })
    },
    fail (res) {
      console.log(res.errMsg)
    }
  })
},
deleteMeasures(e){
  const {type,item,idx}=$.detail(e)
  const pageList=this.data.pageList
  var that=this
  wx.showModal({
    title: '提示',
    content: '是否删除此步骤？',
    success (res) {
      if (res.confirm) {
        pageList.splice(idx,1)
        that.setData({pageList:pageList.map((item,index)=>{
          return {...item, 
          sortOrder:index}
        })})
      } else if (res.cancel) {
        console.log('用户点击取消')
      }
    }
  })
},
// 工作票，操作票---结束
showPopup(e) {
  const {type,item,idx}=$.detail(e)
  this.setData({ isShow: true });
  if(type=='修改'){
    this.setData({ show: true,
      titleType:type,
      controlMeasures:item.controlMeasures,
      desNum:idx+1
   });
  }else{
    this.setData({ show: true,
      titleType:type,
      controlMeasures:'',
      desNum:this.data.pageList.length+1
   });
  }
  
},
onSure(){
  if(this.data.controlMeasures==''||this.data.desNum==''){
    $.toast('请输入完整信息')
  }
  const pageList=this.data.pageList
  if(this.data.titleType=='添加'){
    pageList.splice(this.data.desNum-1, 0, {
      riskDescription:this.data.riskType,
      mainType:this.data.mainType,
      ticketNumber:this.data.ticketNumber,
      riskLevel:this.data.riskLevel,
      operationCategory:this.data.operateType,
      controlMeasures:this.data.controlMeasures})
  }else{
    pageList.splice(this.data.desNum-1, 1, {
      ...pageList[this.data.desNum-1],
      controlMeasures:this.data.controlMeasures})
  }
  this.setData({ isShow: false,
    pageList:pageList.map((item,index)=>{
      return {...item,sortOrder:index}
    }) 
  });
},
onClose() {
  this.setData({ isShow: false });
},
changeDesCont(e){
  console.log(e.detail.value)
  this.setData({controlMeasures:e.detail.value})
},
changeDesNum(e){
  this.setData({desNum:e.detail.value})
},

  
backTicketMark(e){
  const taskContent=this.data.pageList
  Store.dispatch({
    type: 'SET_FROM',
    form: { 
      key: this.data.form.key, // 后台要的更改字段 
      selected:{
        operationProcess:this.data.mainType,
        riskDescription:this.data.riskType,
        operationCategory:this.data.operateType,
        list:taskContent,
      }
    },
  })
  
  $.linkurl('', 'back')
},



})